home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
minix
/
up1510b.tgz
/
up1510b
/
src
/
commands
/
kermit
/
ckcfns.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-19
|
33KB
|
1,040 lines
char *fnsv = "C-Kermit functions, 4E(053) 14 Sep 87";
/* C K C F N S -- System-independent Kermit protocol support functions. */
/* ...Part 1 (others moved to ckcfn2 to make this module small enough) */
/*
4E includes support for Data General systems from Phil Julian of SAS
Institute, and Megamax native Macintosh C compiler support from
Jim Noble of Planning Research Corporation.
*/
/*
Author: Frank da Cruz (SY.FDC@CU20B),
Columbia University Center for Computing Activities, January 1985.
Copyright (C) 1985, Trustees of Columbia University in the City of New York.
Permission is granted to any individual or institution to use, copy, or
redistribute this software so long as it is not sold for profit, provided this
copyright notice is retained.
*/
/*
System-dependent primitives defined in:
ck?tio.c -- terminal i/o
cx?fio.c -- file i/o, directory structure
*/
#include "ckcsym.h" /* Need this for Mac */
#include "ckcker.h" /* Symbol definitions for Kermit */
#include "ckcdeb.h" /* Debug formats, typedefs, etc. */
#ifndef NULL
#define NULL 0
#endif
/* Externals from ckcmai.c */
extern int spsiz, rpsiz, timint, rtimo, npad, ebq, ebqflg, rpt, rptq,
rptflg, capas, keep;
extern int pktnum, prvpkt, sndtyp, bctr, bctu, fmask,
size, osize, maxsize, spktl, nfils, stdouf, warn, timef, spsizf;
extern int parity, speed, turn, turnch,
delay, displa, pktlog, tralog, seslog, xflg, mypadn;
extern long filcnt, ffc, flci, flco, tlci, tlco, tfc, fsize;
extern int tsecs;
extern int deblog, hcflg, binary, savmod, fncnv, local, server, cxseen, czseen;
extern int rq, rqf, sq, wsize, urpsiz, rln;
extern int atcapr, atcapb, atcapu;
extern int lpcapr, lpcapb, lpcapu;
extern int swcapr, swcapb, swcapu;
extern CHAR padch, mypadc, eol, seol, ctlq, myctlq, sstate;
extern CHAR filnam[], sndpkt[], recpkt[], data[], srvcmd[], stchr, mystch;
extern char *cmarg, *cmarg2, *hlptxt, **cmlist;
extern CHAR *srvptr;
extern char *rdatap;
long zchki();
char *strcpy();
CHAR *rpar();
/* Variables local to this module */
static char *memptr; /* Pointer for memory strings */
static char cmdstr[100]; /* Unix system command string */
static int sndsrc; /* Flag for where to send from: */
/* -1: name in cmdata */
/* 0: stdin */
/* >0: list in cmlist */
static int memstr, /* Flag for input from memory string */
first; /* Flag for first char from input */
static CHAR t, /* Current character */
next; /* Next character */
#ifdef datageneral
extern int quiet;
#endif
/* E N C S T R -- Encode a string from memory. */
/* Call this instead of getpkt() if source is a string, rather than a file. */
encstr(s) char* s; {
int m; char *p;
m = memstr; p = memptr; /* Save these. */
memptr = s; /* Point to the string. */
memstr = 1; /* Flag memory string as source. */
first = 1; /* Initialize character lookahead. */
getpkt(spsiz-bctu-3); /* Fill a packet from the string. */
memstr = m; /* Restore memory string flag */
memptr = p; /* and pointer */
first = 1; /* Put this back as we found it. */
}
/* E N C O D E - Kermit packet encoding procedure */
encode(a) CHAR a; { /* The current character */
int a7; /* Low order 7 bits of character */
int b8; /* 8th bit of character */
if (rptflg) { /* Repeat processing? */
if (a == next && (first == 0)) { /* Got a run... */
if (++rpt < 94) /* Below max, just count */
return;
else if (rpt == 94) { /* Reached max, must dump */
data[size++] = rptq;
data[size++] = tochar(rpt);
rpt = 0;
}
} else if (rpt == 1) { /* Run broken, only 2? */
rpt = 0; /* Yes, reset repeat flag & count. */
encode(a); /* Do the character twice. */
if (size <= maxsize) osize = size;
rpt = 0;
encode(a);
return;
} else if (rpt > 1) { /* More than two */
data[size++] = rptq; /* Insert the repeat prefix */
data[size++] = tochar(++rpt); /* and count. */
rpt = 0; /* Reset repeat counter. */
}
}
a7 = a & 0177; /* Isolate ASCII part */
b8 = a & 0200; /* and 8th (parity) bit. */
if (ebqflg && b8) { /* Do 8th bit prefix if necessary. */
data[size++] = ebq;
a = a7;
}
if ((a7 < SP) || (a7==DEL)) { /* Do control prefix if necessary */
data[size++] = myctlq;
a = ctl(a);
}
if (a7 == myctlq) /* Prefix the control prefix */
data[size++] = myctlq;
if ((rptflg) && (a7 == rptq)) /* If it's the repeat prefix, */
data[size++] = myctlq; /* quote it if doing repeat counts. */
if ((ebqflg) && (a7 == ebq)) /* Prefix the 8th bit prefix */
data[size++] = myctlq; /* if doing 8th-bit prefixes */
data[size++] = a; /* Finally, insert the character */
data[size] = '\0'; /* itself, and mark the end. */
}
/* D E C O D E -- Kermit packet decoding procedure */
/* Call with string to be decoded and an output function. */
/* Returns 0 on success, -1 on failure (e.g. disk full). */
decode(buf,fn) CHAR *buf; int (*fn)(); {
unsigned int a, a7, b8; /* Low order 7 bits, and the 8th bit */
rpt = 0; /* Initialize repeat count. */
while ((a = *buf++) != '\0') {
if (rptflg) { /* Repeat processing? */
if (a == rptq) { /* Yes, got a repeat prefix? */
rpt = xunchar(*buf++); /* Yes, get the repeat count, */
a = *buf++; /* and get the prefixed character. */
}
}
b8 = 0; /* Check high order "8th" bit */
if (ebqflg) { /* 8th-bit prefixing? */
if (a == ebq) { /* Yes, got an 8th-bit prefix? */
b8 = 0200; /* Yes, remember this, */
a = *buf++; /* and get the prefixed character. */
}
}
if (a == ctlq) { /* If control prefix, */
a = *buf++; /* get its operand. */
a7 = a & 0177; /* Only look at low 7 bits. */
if ((a7 >= 0100 && a7 <= 0137) || a7 == '?') /* Uncontrollify */
a = ctl(a); /* if in control range. */
}
a |= b8; /* OR in the 8th bit */
if (rpt == 0) rpt = 1; /* If no repeats, then one */
#ifdef NLCHAR
if (!binary) { /* If in text mode, */
if (a == CR) continue; /* discard carriage returns, */
if (a == LF) a = NLCHAR; /* convert LF to system's newline. */
}
#endif
for (; rpt > 0; rpt--) { /* Output the char RPT times */
if ((*fn)(a) < 0) return(-1); /* Send it to the output function. */
ffc++, tfc++; /* Count the character */
}
}
return(0);
}
/* Output functions passed to 'decode': */
putsrv(c) char c; { /* Put character in server command buffer */
*srvptr++ = c;
*srvptr = '\0'; /* Make sure buffer is null-terminated */
return(0);
}
puttrm(c) char c; { /* Output character to console. */
conoc(c);
return(0);
}
putfil(c) char c; { /* Output char to file. */
if (zchout(ZOFILE, c & fmask) < 0) {
czseen = 1; /* If write error... */
debug(F101,"putfil zchout write error, setting czseen","",1);
return(-1);
}
return(0);
}
/* G E T P K T -- Fill a packet data field */
/*
Gets characters from the current source -- file or memory string.
Encodes the data into the packet, filling the packet optimally.
Set first = 1 when calling for the first time on a given input stream
(string or file).
Uses global variables:
t -- current character.
first -- flag: 1 to start up, 0 for input in progress, -1 for EOF.
next -- next character.
data -- the packet data buffer.
size -- number of characters in the data buffer.
Returns the size as value of the function, and also sets global size,
and fills (and null-terminates) the global data array. Returns 0 upon eof.
*/
getpkt(bufmax) int bufmax; { /* Fill one packet buffer */
int i, x; /* Loop index. */
static char leftover[6] = { '\0', '\0', '\0', '\0', '\0', '\0' };
if (first == 1) { /* If first time thru... */
first = 0; /* remember, */
*leftover = '\0'; /* discard any interrupted leftovers, */
x = getchx(&t); /* get first character of file into t, */
if (x == 0) { /* watching out for nul